package com.xinya.taobaosale.ui.fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.xinya.taobaosale.R;
import com.xinya.taobaosale.model.LoadState;
import com.xinya.taobaosale.model.domain.CategoryBean;
import com.xinya.taobaosale.ui.adapter.HomeLayoutAdapter;
import com.xinya.taobaosale.ui.presenter.impl.HomePresenterImpl;
import com.xinya.taobaosale.ui.presenter.interfaces.IHomeCallback;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @ClassName: HomeFragment$
 * @Description: java类作用描述
 * @Author: xinya
 * @CreateDate: 2021/2/1$ 10:57$
 * @UpdateRemark: 更新内容
 * @Version: 1.0
 */
public class HomeFragment extends BaseFragment implements IHomeCallback {

    private static final String TAG = "HomeFragment";
    @BindView(R.id.home_tab)
    TabLayout homeTab;
    @BindView(R.id.home_container)
    ViewPager homeContainer;
    private HomePresenterImpl mHomePresenter;
    private Unbinder mUnbinder;
    private HomeLayoutAdapter mHomeLayoutAdapter;

    @Override
    protected View getRootView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {
        return inflater.inflate(R.layout.fragment_base_home, container, false);
    }

    @Override
    protected int getFragmentLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView(View rootView) {
        mUnbinder = ButterKnife.bind(this, rootView);
        homeTab.setupWithViewPager(homeContainer);
        mHomeLayoutAdapter = new HomeLayoutAdapter(getChildFragmentManager());
        homeContainer.setAdapter(mHomeLayoutAdapter);
    }

    @Override
    protected void initData(View rootView) {
        // 进行分类数据请求
        mHomePresenter.getCategories();
        // 检测tabLayout
        homeTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                Log.d(TAG, "onTabSelected: " + tab.getText());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        setLoadErrorOnclickListener(view -> {
            // 网络错误  再次请求分类数据
            if (mHomePresenter == null) {
                return;
            }
            mHomePresenter.getCategories();
        });
    }

    // categoryList callback
    @Override
    public void onCategoriesLoaded(List<CategoryBean> categoryBeanList) {
        if (categoryBeanList == null || categoryBeanList.size() == 0) {
            return;
        }
        Log.d(TAG, "onCategoriesLoaded: " + categoryBeanList.size());
        // update UI
        mHomeLayoutAdapter.setDataList(categoryBeanList);
        homeContainer.setOffscreenPageLimit(categoryBeanList.size());

    }

    @Override
    protected void registerPresenter() {
        mHomePresenter = new HomePresenterImpl();
        mHomePresenter.register(this);
    }

    @Override
    protected void unRegisterPresenter() {
        if (mHomePresenter != null) {
            mHomePresenter.unRegister(this);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUnbinder.unbind();
    }

    @Override
    public void onLoadSuccess(int id) {
        switchLoadState(LoadState.SUCCESS);
        Log.d(TAG, "onLoadSuccess: ");
    }

    @Override
    public void onLoadError(int id) {
        switchLoadState(LoadState.ERROR);
        Log.d(TAG, "onLoadError: ");
    }

    @Override
    public void onLoadEmpty(int id) {
        switchLoadState(LoadState.EMPTY);
        Log.d(TAG, "onLoadEmpty: ");
    }

    @Override
    public void onLoading(int id) {
        switchLoadState(LoadState.LOADING);
        Log.d(TAG, "onLoading: ");
    }
}
